package com.xuequ.answerme.settings;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xuequ.answerme.R;
import com.xuequ.answerme.beans.FeedbackBean;
import com.xuequ.answerme.beans.UserBean;
import com.xuequ.answerme.manager.FeedbackManager;
import com.xuequ.answerme.manager.UserManager;
import com.xuequ.answerme.tools.FileTools;
import com.xuequ.answerme.tools.Tools;

import java.io.File;

import cn.bmob.v3.BmobUser;
import de.hdodenhof.circleimageview.CircleImageView;
import me.drakeet.materialdialog.MaterialDialog;

/**
 * Created by Shijie on 2015/10/12.
 */
public class SettingsActivity extends AppCompatActivity {

    private static final String IMAGE_FILE_NAME = "header.jpg";
    private static final int START_PERSONAL_INFO = 0;
    private static final int START_PERSONAL_INFO_CHANGED = 1;
    private static final int START_PERSONAL_INFO_UNCHANGED = 2;

    private Toolbar toolbar;
    private LinearLayout linearLayout;

    private CardView personalInfoCard;
    private CircleImageView avatarImg;
    private TextView usernameText;
    private TextView signatureText;

    private CardView feedbackCard;
    private CardView aboutUsCard;

    private UserBean userBean;

    private UserManager userManager;
    private FeedbackManager feedbackManager;

    private MaterialDialog materialDialogFeedback;
    private MaterialDialog materialDialogAboutUs;

    private View contentViewFeedback;
    private TextInputLayout feedbackTextInput;
    private TextView mFeedbackOKText;
    private TextView mFeedbackCancelText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        initViews();//初始化控件和数据
    }

    /**
     * 初始化控件
     */
    private void initViews() {

        if (userManager == null) {
            userManager = new UserManager();
        }
        if (feedbackManager == null) {
            feedbackManager = new FeedbackManager();
        }

        toolbar = (Toolbar) findViewById(R.id.settings_tool_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        userBean = BmobUser.getCurrentUser(this, UserBean.class);
        Log.i("Settings", "username" + userBean.getUsername());

        personalInfoCard = (CardView) findViewById(R.id.settings_personal_info_card);
        personalInfoCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivityForResult(new Intent(SettingsActivity.this, PersonalInfoActivity.class), START_PERSONAL_INFO);
                startActivity(new Intent(SettingsActivity.this, PersonalInfoActivity.class));
            }
        });
        avatarImg = (CircleImageView) findViewById(R.id.settings_personal_info_avatar_img);
        Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + FileTools.AVATAR_NAME);
        avatarImg.setImageBitmap(bitmap);

        usernameText = (TextView) findViewById(R.id.settings_personal_info_username_text);
        usernameText.setText(BmobUser.getCurrentUser(this, UserBean.class).getUsername());
        signatureText = (TextView) findViewById(R.id.settings_personal_info_signature_text);
        signatureText.setText(BmobUser.getCurrentUser(this, UserBean.class).getSignature());

        feedbackCard = (CardView) findViewById(R.id.settings_change_feedback_card);
        feedbackCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //意见反馈
                showFeedback();
            }
        });

        aboutUsCard = (CardView) findViewById(R.id.settings_change_about_card);
        aboutUsCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //show about us
            }
        });

    }

    /**
     * 显示反馈框
     */
    private void showFeedback() {
        contentViewFeedback = View.inflate(this, R.layout.settings_feedback_layout, null);

        feedbackTextInput = (TextInputLayout) contentViewFeedback.findViewById(R.id.settings_feedback_textInput);
        feedbackTextInput.setHint("你们的话是对我们的支持:)");

        mFeedbackOKText = (TextView) contentViewFeedback.findViewById(R.id.settings_feedback_ok_text);
        mFeedbackOKText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(feedbackTextInput.getEditText().getText().toString())) {
                    //push feedback
                    FeedbackBean feedbackBean = new FeedbackBean();
                    feedbackBean.setAdviser(userBean);
                    feedbackBean.setFeedback_content(feedbackTextInput.getEditText().getText().toString());

                    //manager
                    feedbackManager.sendFeedback(SettingsActivity.this, feedbackBean, new FeedbackManager.SendFeedbackListener() {
                        @Override
                        public void sendFeedbackOnSuccess() {
                            materialDialogFeedback.dismiss();
                        }

                        @Override
                        public void sendFeedbackOnFailure(String failureMsg) {

                        }
                    });
                }
            }
        });

        mFeedbackCancelText = (TextView) contentViewFeedback.findViewById(R.id.settings_feedback_cancel_text);
        mFeedbackCancelText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                materialDialogFeedback.dismiss();
            }
        });

        materialDialogFeedback = new MaterialDialog(this).setView(contentViewFeedback);
        materialDialogFeedback.show();
    }

    @Override
    protected void onDestroy() {
        Log.i("Settings", "------------>----------->------->-------->destroy");
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        Log.i("Settings", "------------>----------->------->-------->start");
        super.onStart();
    }

    @Override
    protected void onRestart() {
        Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/" + FileTools.AVATAR_NAME);
        avatarImg.setImageBitmap(bitmap);
        usernameText.setText(BmobUser.getCurrentUser(this, UserBean.class).getUsername());
        if (!TextUtils.isEmpty(BmobUser.getCurrentUser(this, UserBean.class).getSignature())) {
            signatureText.setText(BmobUser.getCurrentUser(this, UserBean.class).getSignature());
        }
        Log.i("Settings", "------------>----------->------->-------->restart");
        super.onRestart();
    }

    @Override
    protected void onPause() {
        Log.i("Settings", "------------>----------->------->-------->pause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.i("Settings", "------------>----------->------->-------->stop");
        super.onStop();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
